package com.xmkj.bearbiz.home;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.Entity.SearchEntity;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.SearchListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.diver.DividerGridItemDecoration;
import com.xmkj.bearbiz.flow.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/11/13.
 */

public class SearchActivity extends BaseMvpActivity {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.flow_recent)
    TagFlowLayout flowRecent;

    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.ll_search)
    LinearLayout ll_search;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;

    private List<String> recentVals = new ArrayList<>();
    private String keyword;
    private SearchListAdapter adapter;


    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
     /*   if (!EmptyUtils.isEmpty(uid)) {
            getSearchDatas();
        }*/
        setRecyclerView();
        attachClickListener(tvCancel);
        attachClickListener(ivSearch);

    }

    private void getSearchDatas() {


       /* CommonSubscriber<SearchBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                SearchBean bean = (SearchBean) o;
                if (bean.now.size() > 0) {
                    for (int i = 0; i < bean.now.size(); i++) {
                        recentVals.add(bean.now.get(i));
                    }
                }
                if (bean.history.size() > 0) {
                    for (int i = 0; i < bean.history.size(); i++) {
                        historyVals.add(bean.history.get(i));
                    }
                }
                if (bean.now.size() > 0 && bean.now.size() > 0) {
                    historyVals.add("清空");
                }

                initRecentFlowLayout();
                initHistroyFlowLayout();
            }

            @Override
            public void onError(String e, int code) {
                showToastMsg(e);
            }
        });
        GetSearchDatasMethods.getInstance().history_search(subscriber, uid, hashid);
        rxManager.add(subscriber);*/
    }

/*    private void initHistroyFlowLayout() {
        flowHistory.setAdapter(new com.xmkj.bearbiz.flow.TagAdapter(historyVals) {
            @Override
            public View getView(com.xmkj.bearbiz.flow.FlowLayout parent, int position, Object o) {
                TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tv_flag,
                        flowHistory, false);
                tv.setText(o.toString());
                return tv;
            }
        });

        flowHistory.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, com.xmkj.bearbiz.flow.FlowLayout parent) {
                if (position == historyVals.size() - 1) {
                    deleteHistoryRecord();

                } else {
//                    Intent intent = new Intent(context,SearchRerultActivity.class);
//                    intent.putExtra(SearchRerultActivity.CONTENT,historyVals.get(position));
//                    startActivity(intent);
                    keyword = historyVals.get(position);
                    gotoSearch();
                }
                return true;
            }




        });
        flowHistory.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
//                showToastMsg("choose:" + selectPosSet.toString());
//                attrid = (int) selectPosSet.toArray()[0];

            }
        });

    }*/

    private void gotoSearch() {
        ll_search.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
        mPageIndex = 1;
        setRecyclerView();
        goToHttpReq();

    }

    private void deleteHistoryRecord() {
        /*showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                historyVals.clear();
                recentVals.clear();
                initRecentFlowLayout();
                initHistroyFlowLayout();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        DeleteSearchDatasMethods.getInstance().del_search(subscriber, uid, hashid);
        rxManager.add(subscriber);*/

    }

    private void initRecentFlowLayout() {
        flowRecent.setAdapter(new com.xmkj.bearbiz.flow.TagAdapter(recentVals) {
            @Override
            public View getView(com.xmkj.bearbiz.flow.FlowLayout parent, int position, Object o) {
                TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tv_flag,
                        flowRecent, false);
                tv.setText(o.toString());
                return tv;
            }
        });

        flowRecent.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, com.xmkj.bearbiz.flow.FlowLayout parent) {
                keyword = recentVals.get(position);
                gotoSearch();
                return true;
            }

        });
        flowRecent.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
//                showToastMsg("choose:" + selectPosSet.toString());
//                attrid = (int) selectPosSet.toArray()[0];

            }
        });

    }

    @Override
    protected void setNavigation() {
        super.setNavigation();
    }

    @Override
    protected void onViewClicked(View view) {
        super.onViewClicked(view);
        if (view.getId() == tvCancel.getId()) {
            onBackPressed();
        } else if (view.getId() == ivSearch.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(etSearch))) {
                showToastMsg("请输入搜索内容");
                return;
            }

            keyword = getEditTextStr(etSearch);
            gotoSearch();

        }
    }





    @Override
    public void bindKnife() {
        super.bindKnife();
        ButterKnife.bind(this);
    }

    private void setRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerGridItemDecoration(context));

      /*  IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerView);
        recyclerView.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));*/
        recyclerView.setHasFixedSize(true);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setRefreshing(false);
        adapter = new SearchListAdapter(context, bean);
        recyclerView.setAdapter(adapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                goToHttpReq();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerView.loadMoreComplete();
                    recyclerView.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                goToHttpReq();
            }
        });
    }

    private List<SearchEntity.ListsBean> bean = new ArrayList<>();

    private void goToHttpReq() {
        adapter.clear();
        bean.clear();
        showProgressingDialog();
        CommonSubscriber<SearchEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                SearchEntity beans = (SearchEntity) o;
                List<SearchEntity.ListsBean> beanList = beans.lists;
                if(EmptyUtils.isNotEmpty(beans)&&beans.lists.size()==0){
                    showToastMsg("暂无符合的数据");
                }
                recyclerView.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerView.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerView.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerView.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg("暂无符合的数据");
                adapter.notifyDataSetChanged();
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        SearchListMethods.getInstance().searchJingpai(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex,keyword);
        rxManager.add(subscriber);
    }
}